package com.github.tangyi.user.controller;

import com.github.pagehelper.PageInfo;
import com.github.tangyi.api.user.model.SysSmsRecord;
import com.github.tangyi.common.base.BaseController;
import com.github.tangyi.common.log.OperationType;
import com.github.tangyi.common.log.SgLog;
import com.github.tangyi.common.model.R;
import com.github.tangyi.common.utils.SysUtil;
import com.github.tangyi.user.service.sys.SysSmsRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

@Slf4j
@RestController
@AllArgsConstructor
@Tag(name = "短信发送记录管理")
@RequestMapping("/v1/record")
public class SysSmsRecordController extends BaseController {

    private final SysSmsRecordService sysSmsRecordService;

    @GetMapping("/list")
	@Operation(summary = "查询短信发送记录列表")
    public R<PageInfo<SysSmsRecord>> list(@RequestParam Map<String, Object> condition,
		@RequestParam(value = PAGE, required = false, defaultValue = PAGE_DEFAULT) int pageNum,
		@RequestParam(value = PAGE_SIZE, required = false, defaultValue = PAGE_SIZE_DEFAULT) int pageSize) {
	    return R.success(sysSmsRecordService.findPage(condition, pageNum, pageSize));
    }

    @GetMapping(value = "/{id}")
	@Operation(summary = "获取短信发送记录详细信息")
    public R<SysSmsRecord> get(@PathVariable("id") Long id) {
        return R.success(sysSmsRecordService.get(id));
    }

    @PostMapping
	@Operation(summary = "新增短信发送记录")
	@SgLog(value = "新增短信发送记录", operationType = OperationType.INSERT)
    public R<Boolean> add(@RequestBody @Valid SysSmsRecord sysSmsRecord) {
        sysSmsRecord.setCommonValue();
        return R.success(sysSmsRecordService.insert(sysSmsRecord) > 0);
    }

    @PutMapping("{id}")
	@Operation(summary = "修改短信发送记录")
	@SgLog(value = "修改短信发送记录", operationType = OperationType.UPDATE)
    public R<Boolean> update(@PathVariable("id") Long id, @RequestBody @Valid SysSmsRecord sysSmsRecord) {
        sysSmsRecord.setId(id);
        sysSmsRecord.setCommonValue(SysUtil.getUser(), sysSmsRecord.getTenantCode());
        return R.success(sysSmsRecordService.update(sysSmsRecord) > 0);
    }

	@DeleteMapping("{id}")
	@Operation(summary = "删除短信发送记录")
	@SgLog(value = "删除短信发送记录", operationType = OperationType.DELETE)
    public R<Boolean> delete(@PathVariable("id") Long id) {
        SysSmsRecord sysSmsRecord = sysSmsRecordService.get(id);
        sysSmsRecord.setCommonValue();
        return R.success(sysSmsRecordService.delete(sysSmsRecord) > 0);
    }
}
